<template>
	<view>
		<view v-for="(item,index) in goodsList" :key="index" @click="gotoDetail(item)">
				<my-goods :item="item"> </my-goods>	
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj:{
					query:'',
					cid:'',
					pagenum:1,
					pagesize:10
				},
				goodsList:[],
				total:0,
				isLoading:false
			};
		},
		onLoad(options){
				console.log(options);
				this.queryObj.query=options.query|| ''
				this.queryObj.cid=options.cid || ''
				this.getGoodsList()
		},
		methods:{
				async	getGoodsList(cb){
						//获取商品列表
						// 打开节流
					 this.isLoading=true
					 // console.log(await uni.$http.get(`api/public/v1/goods/search`,this.queryObj));
					 const {data:res} =  await uni.$http.get(`api/public/v1/goods/search`,this.queryObj)
					 console.log(res);
					 this.isLoading=false
					 cb && cb()
					 if(!res.meta) return uni.$showMsg()
					 if( res.meta.status!=200 || !res.meta) return uni.$showMsg()
					 this.goodsList=[...this.goodsList,...res.message.goods]
					 this.total=res.message.total
					 //关闭节流 
					},
					gotoDetail(goods){
						uni.navigateTo({
							url:`/subpkg/goods_detail/goods_detail?goods_id=${goods.goods_id}`
						})
					}
		},
		onReachBottom(){
			//如果当前请求还未结束
			 console.log(this.total);
			  if(this.queryObj.pagenum*this.queryObj.pagesize>=this.total) return uni.$showMsg('数据已经加载完毕')
			  if(this.isLoading==true) return
				this.queryObj.pagenum+=1
				this.getGoodsList()
		},
		onPullDownRefresh(){
			//重置关键数据
			this.queryObj.pagenum=1,
			this.total=0,
			this.isLoading=false,
			this.goodsList=[]
			//重新发起数据请求
			this.getGoodsList(()=>{
				uni.stopPullDownRefresh()
			})
		},
		
		
	
	}
</script>

<style lang="scss">

</style>
